package edu.ayit.controller.admin;

import edu.ayit.pojo.Blog;
import edu.ayit.pojo.Tag;
import edu.ayit.pojo.User;
import edu.ayit.service.BlogService;
import edu.ayit.service.TagService;
import edu.ayit.service.TypeService;
import edu.ayit.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;


    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog,
                        Model model){
        model.addAttribute("types",typeService.getList());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog,
                        Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        //返回一个blogList片段，用于局部刷新
        return "admin/blogs::blogList";
    }

    //跳转到增加博客页面
    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("tags",tagService.getList());
        model.addAttribute("types",typeService.getList());
        model.addAttribute("blog",new Blog());
        return "admin/blogs-input";
    }

    //修改博客
    @GetMapping("/blogs/{id}/input")
    public String editInput(Model model,@PathVariable Long id){
        model.addAttribute("tags",tagService.getList());
        model.addAttribute("types",typeService.getList());
        Blog blog = blogService.getBlog(id);
        //注意，blog的tagIds属性并未保存在数据库，需要经过处理才能获取
        List<Tag> tags = blog.getTags();
        StringBuffer ids = new StringBuffer();
        if(tags!=null){
            boolean flag = false;//判断是否是最后一个
            for (Tag tag : tags) {
                if(flag){
                    ids.append(",");
                }else{
                    flag=true;
                }
                ids.append(tag.getId());
            }
        }

        blog.setTagIds(ids.toString());
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    //提交博客
    @PostMapping("/blogs")
    public String add(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.getList(blog.getTagIds()));
        System.out.println("--------------------------------------------------");
        System.out.println(blog);
        Blog saveBlog;

        if(blog.getId()==null){
            saveBlog = blogService.saveBlog(blog);
        }else {
            saveBlog = blogService.updateBlog(blog,blog.getId());
        }
        System.out.println(saveBlog);
        System.out.println("----------------------------------------");
        if(saveBlog!=null){
            attributes.addFlashAttribute("message","操作成功");
        }else {
            attributes.addFlashAttribute("message","操作失败");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }
}
